/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Xiaobao Zeng<zengxiaobao@skyworth.com>
 * Created Time: 2017-01-14 17:26:56
 * Last Modified: 2017-01-14 20:42:24
 * File Name: getTestUserInfo.go
 * Description:
 */

package controllers

import (
	"encoding/json"
	"io/ioutil"
	"os"
	"path/filepath"

	log "github.com/cihub/seelog"
)

func fileExists(name string) bool { //判断路径是否存在
	if _, err := os.Stat(name); err != nil {
		if os.IsNotExist(err) {
			return false
		}
	}
	return true
}

func getTestUserInfoConfPath(path string) (dir, filePath string) { //获取配置文件路径
	levelPath, _ := filepath.Abs(filepath.Dir(os.Args[0]))
	dbFilePath, _ := filepath.Abs(filepath.Join(levelPath, path)) //"../conf/fingertrip_invitecode_exchange_level.conf"))
	dbDir := filepath.Dir(dbFilePath)
	return dbDir, dbFilePath
}

//获取配置文件的所有信息
func getTestUserInfoConf(path string) interface{} { ///读取配置文件
	_, dbFilePath := getTestUserInfoConfPath(path)
	if !fileExists(dbFilePath) {
		log.Warnf("%s is not exists", dbFilePath)
		return nil
	}
	bytes, err := ioutil.ReadFile(dbFilePath)
	if err != nil {
		log.Warnf("read from %s err, msg: %s", dbFilePath, err.Error())
		return nil
	}
	resp := make(map[string]interface{})
	if err := json.Unmarshal(bytes, &resp); err == nil {
		return resp
	} else {
		log.Warnf("getTestUserInfoConf, json err, err:%s", err.Error())
	}
	return nil
}
